<template>
  <div>
    <div class="system_set_view_1">
      <div v-for="(title, i) in titleList" :key="i" class="column_item">
        <div class="popper_title_1">{{ title }}</div>
        <div class="item_list_1">
          <div class="item_title" @click="routeClick(item)" v-for="(item, index) in routeList[title]" :key="index">
            <!-- 单据查询做特殊样式处理 -->
            <span v-if="item.title !== '单据查询'">{{ item.title }}</span>
            <p v-else>{{ item.title }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import subMenu from './submenu'
import { getAction } from '@/api/manage'
import { handleOtherPerms } from '@/utils/index'
import Vue from 'vue'
import { handlePermissions, handlePagePermissions } from '../../../utils'
export default {
  inject: ['remove', 'getCurrentPages'],
  name: 'submenu',
  components: {},
  props: {
    path: {
      type: String,
      default: '',
    },
  },
  watch: {
    '$store.getters.openAccountValue'(val) {
      this.openAccount = val
      if (val === 3) {
        this.isOpenAccount = true
      } else {
        this.isOpenAccount = false
      }
    },
  },
  data() {
    return {
      routeList: [],
      titleList: [],
      dialogVisible: false,
      isOpenAccount: false, // 开账状态
      openAccount: 0, // 开账状态
      printPermissions: false, //打印模板设置权限
    }
  },
  computed: {
    cashPermissions() {
      return handlePermissions('现金银行统计表')
    },
  },
  mounted() {
    this.printPermissions = handleOtherPerms('打印模板设置')
    this.openAccount = this.$store.getters.openAccountValue
    if (this.openAccount === 3) {
      this.isOpenAccount = true
    } else {
      this.isOpenAccount = false
    }
    let data = this.getMenuData()
    let tList = [] //第二层标题
    let rList = {} //第二次路径
    if (data && data.children && data.children.length > 0) {
      //配置数据
      for (let index = 0; index < data.children.length; index++) {
        const element = data.children[index]
        let title = element.meta.title
        tList.push(title)
        let children = element.children || []
        let cList = [] //存放第三层路径
        for (let i = 0; i < children.length; i++) {
          const e = children[i]
          if (!e.hidden) {
            cList.push({
              title: e.meta.title,
              path: e.path,
            })
          }
        }
        rList[title] = cList
      }
      this.titleList = tList
      this.routeList = rList
    } else {
      //本地数据
      this.titleList = subMenu.routeMap[this.path].titles
      this.routeList = subMenu.routeMap[this.path]
    }
  },
  methods: {
    getMenuData() {
      let arr = this.$store.state.user.permissionList
      for (let index = 0; index < arr.length; index++) {
        const element = arr[index]
        if (element.path == this.path) {
          return element
        }
      }
    },
    routeClick(item) {
      console.log('item.path', item.path)
      console.log(this.getCurrentPages());
      console.log(this.$route);
      
      this.printPermissions = handleOtherPerms('打印模板设置')

      if (item.path == this.$route.path) {
        this.$emit('pathChange', 0)
        return
      }
      // 账套过期不允许进入其他页面
      if (Vue.ls.get('account-no-money')) {
        return this.$elMessage.warning('续费未完成不能操作!')
      }
      if (!this.isOpenAccount) {
        // 基本信息的资料都可以操作
        if (
          [
            '/initialAccountSetup',
            '/baseInfomation/material/commodityInfo',
            '/baseInfomation/material/transactionCompanyInfo',
            '/baseInfomation/material/staffInfo',
            '/baseInfomation/material/warehouseInfo',
            '/baseInfomation/material/logisticsInfo',
            '/baseInfomation/material/accountInfo',
            '/baseInfomation/material/materialsInfo',
          ].includes(item.path)
        ) {
          // 未选择开账不能进入基本信息页面
          if (this.openAccount === 0) {
            return this.$elMessage.warning('请先选择开账方式!')
          }
          return this.toPath(item)
        } else if (
          [
            '/baseInfomation/beginningOfPeriod/inventoryEntry',
            '/baseInfomation/beginningOfPeriod/arAndapEntry',
            '/baseInfomation/beginningOfPeriod/accountBalanceEntry',
          ].includes(item.path)
        ) {
          // 期初菜单需要处理
          if (this.openAccount === 0) {
            return this.$elMessage.warning('请先选择开账方式!')
          }
        } else {
          return this.$elMessage.warning('未开账不能操作!')
        }
      }
      if (
        [
          '/baseInfomation/printSetting/printSet',
          '/baseInfomation/printSetting/printSetRecord',
          '/baseInfomation/printSetting/printSetStatic',
          '/baseInfomation/printSetting/printSetStock',
          '/baseInfomation/printSetting/printSetSettlement',
          '/baseInfomation/printSetting/templateSet',
          '/baseInfomation/printSetting/library',
        ].includes(item.path) &&
        !this.printPermissions
      ) {
        return this.$elMessage.warning('未拥有打印模板设置权限！!')
      }
      // 现金银行统计表拦截是否有录入权限
      if (item.path == '/reportStatistics/financeReport/cashBankStatistics' && !this.cashPermissions.LuRu) {
        return this.$elMessage.warning('没有权限！')
      }
      //没有当前页面权限
      if (!handlePagePermissions(item.title)) {
        return this.$elMessage.warning('没有权限！')
      }

      //有补货分析生成的页面时切换
      if (['/purchaseManage/purchaseBill/billPurchaseWarehousing', '/purchaseManage/purchaseBill/billPurchaseOrder'].includes(item.path)) {
        let pageList = this.getCurrentPages()
        for (let index = 0; index < pageList.length; index++) {
          const element = pageList[index];
          if (element.fullPath.includes(item.path) && element.fullPath.includes('from=replenish')) {
            this.$router.push(element.fullPath)
            return 
          } 
        }
      }

      this.toPath(item)
    },
    toPath(item) {
      let path = item.path
      if (path == '/otherWarehouseType') {
        this.$emit('pathChange', 1)
      } else if (path == '/initialAccountSetup') {
        this.$emit('pathChange', 3)
      } else if (path == '/otherDeliveryType') {
        this.$emit('pathChange', 2)
      } else {
        this.$emit('pathChange', 0)
        this.$router.push(
          {
            path: item.path,
          },
          () => {},
          () => {}
        )
      }
    },
  },
}
</script>
<style lang="less" scoped>
.system_set_view_1 {
  width: 100%;
  // height: 260px;
  background: #042a4d;
  padding: 7px 37px 20px;
  display: flex;
}

.column_item {
  width: 120px;
  margin-right: 20px;
}

.popper_title_1 {
  width: 100%;
  font-size: 14px;
  font-family: PingFang SC;
  font-weight: 500;
  line-height: 25px;
  color: #1b94fb;
  border-bottom: #1b94fb solid 1px;
  line-height: 40px;
  margin-bottom: 20px;
}

.item_title {
  height: 30px;
  width: 150px;
  color: #1b94fb;
  cursor: pointer;

  > p {
    display: flex;
    width: 80px;
    height: 30px;
    border: 1px solid #e4f6fe;
    border-radius: 4px;
    justify-content: center;
    align-items: center;
    font-size: 12px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 17px;
    color: #e3f6ff;
    margin-top: 15px;
  }
}

.item_list_1 {
  // display: flex;
  // flex-direction: column;
  // flex-wrap: wrap;
  // height: 160px;

  & > div:hover {
    color: #fff;
  }
}
</style>

